#include "queue.h"


void QueueInit(queue* ps)//初始化队列
{
  assert(ps);
  ps->head=NULL;
  ps->back=NULL;

}

void QueuePush(queue* ps,Class x)//入队
{
  assert(ps);

  QNode* tmp=(QNode*)malloc(sizeof(QNode));
  tmp->a=x;
  tmp->next=NULL;

  if(ps->head==NULL)
  {
    ps->back=ps->head=tmp;
  }else
  {
    ps->back->next=tmp;
    ps->back=tmp;
  }
}

void QueuePop(queue* ps)//出队列
{
  assert(ps);

  if(!QueueEmpty(ps))
  {
    QNode* tmp=ps->head;
    if(ps->head==ps->back)
    {
      ps->head=ps->back=NULL;
    }else
    {
      ps->head=ps->head->next;
    }

    free(tmp);
  }else
  {
    assert(0);
  }
}

Class QueueFront(queue* ps)//取头部数据
{
  assert(ps);

  if(!QueueEmpty(ps))
  {
    return ps->head->a;
  }else 
  {
    assert(0);
  }
}

Class QueueBack(queue* ps)//取尾部的数据
{
  assert(ps);

  if(!QueueEmpty(ps))
  {
    return ps->back->a;
  }else
  {
    assert(0);
  }
}

int QueueSize(queue* ps)//获取个数
{
  assert(ps);

  if(!QueueEmpty(ps))
  {
    int i=1;
    QNode* cur=ps->head;
    while(cur!=ps->back)
    {
      cur=cur->next;
      i++;
    }
    return i;
  }else
  {
    assert(0);
  }
}

int QueueEmpty(queue* ps)//判空
{
  assert(ps);

  return ps->head==NULL;
}
void QueueDestroy(queue* ps)//销毁
{
  assert(ps);

  if(!QueueEmpty(ps))
  {
    QNode* cur=ps->head;
    QNode* tmp=NULL;
    while(cur)
    {
      tmp=cur;
      cur=cur->next;
      free(tmp);
    }
   ps->head=ps->head=NULL;    
    
  }else
  {
    printf("以销毁队列");
  }
}




